package com.yinxin.JDK.DelayQueue;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/7/716:23
 */
public class MyDelay implements Delayed {
   /**
    * 延迟时间
    */
   @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
   private Long time;
   String name;


   public MyDelay(String name, Long time, TimeUnit unit) {
      this.name = name;
      this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
   }

   public Long getTime() {
      return time;
   }

   public String getName() {
      return name;
   }

   //获取延时时间
   @Override
   public long getDelay(TimeUnit unit) {
      return time - System.currentTimeMillis();
   }

   //对延时队列中的元素进行排序
   @Override
   public int compareTo(Delayed o) {
      MyDelay myDelay = ((MyDelay) o);
      return this.time.compareTo(myDelay.getTime());
   }
}
